import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/data/common/chart_data.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'controller.dart';

///统计分析
class StatisticalAnalysisPage extends StatelessWidget {
  StatisticalAnalysisPage({super.key});

  final controller = Get.put(StatisticalAnalysisController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        titleText: '统计分析',
        // actions: [
        //   Visibility(
        //     visible:
        //         controller.currentMainCompany == controller.userCompanyName,
        //     child: GetBuilder<StatisticalAnalysisController>(
        //         id: 'tabSelect',
        //         init: controller,
        //         builder: (_) {
        //           return _company();
        //         }),
        //   ),
        // ],
      ),
      body: Container(
        color: const Color(0xFFF7FBFE),
        padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 14.w),
        child: ListView(
          children: [
            GetBuilder<StatisticalAnalysisController>(
                id: 'top',
                init: controller,
                builder: (_) {
                  return CustomTitleChartPage(
                    titleColor: const Color(0xFF1847E3),
                    leftIcon: SvgPicture.asset(
                      'assets/svg/icon_duty_attendance_rate_statistics.svg',
                    ),
                    title: '值班签到率统计',
                    child: _tobWidget(),
                  );
                }),
            AppGaps.vGap16,
            GetBuilder<StatisticalAnalysisController>(
                id: 'center',
                init: controller,
                builder: (_) {
                  return CustomTitleChartPage(
                    leftIcon: SvgPicture.asset(
                      'assets/svg/icon_enterprise_risk_statistics.svg',
                    ),
                    titleRightWidget: _company(1),
                    title: '企业风险统计',
                    titleColor: const Color(0xFF1847E3),
                    child: _centerWidget(),
                  );
                }),
            AppGaps.vGap16,
            GetBuilder<StatisticalAnalysisController>(
                id: 'bottom',
                init: controller,
                builder: (controller) {
                  return CustomTitleChartPage(
                    titleColor: const Color(0xFF1847E3),
                    leftIcon: SvgPicture.asset(
                      'assets/svg/icon_statistical_analysis_monitoring_equ.svg',
                    ),
                    title: '监测设备统计分析',
                    titleRightWidget: _company(2),
                    child: _bottomWidget(),
                  );
                }),
            AppGaps.vGap30,
          ],
        ),
      ),
    );
  }

  Widget _tobWidget() {
    return Column(
      children: [
        Row(
          children: [
            Container(
              padding: EdgeInsets.only(
                  left: 12.w, top: 4.w, right: 12.w, bottom: 4.w),
              height: AppDimens.dp32,
              decoration: BoxDecoration(
                  color: controller.pieChartMonthUp
                      ? const Color(0xFF1847E3)
                      : const Color(0xFFEEF0F7),
                  // border: Border.all(color: const Color(0xffDCE1EA)),
                  borderRadius: BorderRadius.circular(6)),
              child: Center(
                child: Text(
                  '${controller.getUpFirstDayOfMonth(controller.currentUpMonthUp).month}月数据',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: controller.pieChartMonthUp
                        ? Colors.white
                        : const Color(0xFF3F425B),
                  ),
                ),
              ),
            ).clickOnce(controller.onClickMonthUp),
            AppGaps.hGap10,
            Container(
              padding: EdgeInsets.only(
                  left: 12.w, top: 4.w, right: 12.w, bottom: 4.w),
              height: AppDimens.dp32,
              decoration: BoxDecoration(
                  color: controller.pieCharTimeRangeUp
                      ? const Color(0xFF1847E3)
                      : const Color(0xFFEEF0F7),
                  // border: Border.all(color: const Color(0xffDCE1EA)),
                  borderRadius: BorderRadius.circular(6)),
              child: Center(
                child: Text(
                  controller.endDateUIUp,
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: controller.pieCharTimeRangeUp
                        ? Colors.white
                        : const Color(0xFF3F425B),
                  ),
                ),
              ),
            ).clickOnce(controller.selectTimeUp),
          ],
        ),
        _tobWidgetTop(),
        AppGaps.vGap10,
        Container(
          color: const Color(0xFF587DF5).withOpacity(0.2),
          height: 1.w,
        ),
        AppGaps.vGap16,
        Row(
          children: [
            SvgPicture.asset(
              'assets/svg/icon_statistics_duty_plan_execution_rate.svg',
            ),
            AppGaps.hGap8,
            Text(
              '值班计划执行率统计',
              style: TextStyle(
                fontWeight: FontWeight.w500,
                color: const Color(0xFF1847E3),
                fontSize: 15.sp,
              ),
            ),
          ],
        ),
        AppGaps.vGap16,
        Row(
          children: [
            Container(
              padding: EdgeInsets.only(
                  left: 12.w, top: 4.w, right: 12.w, bottom: 4.w),
              height: AppDimens.dp32,
              decoration: BoxDecoration(
                  color: controller.pieChartMonthDown
                      ? const Color(0xFF1847E3)
                      : const Color(0xFFEEF0F7),
                  // border: Border.all(color: const Color(0xffDCE1EA)),
                  borderRadius: BorderRadius.circular(6)),
              child: Center(
                child: Text(
                  '${controller.getUpFirstDayOfMonth(controller.currentUpMonthDown).month}月数据',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: controller.pieChartMonthDown
                        ? Colors.white
                        : const Color(0xFF3F425B),
                  ),
                ),
              ),
            ).clickOnce(controller.onClickMonthDown),
            AppGaps.hGap10,
            Container(
              padding: EdgeInsets.only(
                  left: 12.w, top: 4.w, right: 12.w, bottom: 4.w),
              height: AppDimens.dp32,
              decoration: BoxDecoration(
                  color: controller.pieCharTimeRangeDown
                      ? const Color(0xFF1847E3)
                      : const Color(0xFFEEF0F7),
                  // border: Border.all(color: const Color(0xffDCE1EA)),
                  borderRadius: BorderRadius.circular(6)),
              child: Center(
                child: Text(
                  controller.endDateUIDown,
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: controller.pieCharTimeRangeDown
                        ? Colors.white
                        : const Color(0xFF3F425B),
                  ),
                ),
              ),
            ).clickOnce(controller.selectTimeDown),
          ],
        ),
        _tobWidgetDown(),
      ],
    );
  }

  Widget _centerWidget() {
    return Column(
      children: [
        Text(
          '安全生产',
          style: TextStyle(
            fontWeight: FontWeight.w500,
            color: const Color(0xFF1D3452),
            fontSize: 15.sp,
          ),
        ),
        _buildRiskAnalysisInit(),
        Text(
          '环境监测',
          style: TextStyle(
            fontWeight: FontWeight.w500,
            color: const Color(0xFF1D3452),
            fontSize: 15.sp,
          ),
        ),
        _buildRiskAnalysisLeft(),
      ],
    );
  }

  /// 风险分析图表（初始）
  Widget _buildRiskAnalysisInit() {
    final annoCenter = Container(
      width: 56.w,
      height: 56.w,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(50.w),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            '安全生产',
            style: TextStyle(
              fontSize: 12.w,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA0A7B5),
            ),
          ),
          Text(
            '${controller.initRiskCount}',
            style: TextStyle(
                fontSize: 16.w,
                fontWeight: FontWeight.w400,
                color: const Color(0xFF495061)),
          ),
        ],
      ),
    );
    return SizedBox(
      height: 180.w,
      child: SfCircularChart(
        legend: Legend(
          isVisible: true,
          height: 170.w.toString(),
          position: LegendPosition.right,
          overflowMode: LegendItemOverflowMode.none,
          legendItemBuilder: (String name, dynamic series, dynamic point, int seriesIndex) {
            final item = controller.initRiskData[seriesIndex];
            final child = _buildLegendItem(
              item.color,
              name,
              item.y.toInt(),
              height: 19.w,
              top: 3.w,
            ).clickOnce(() {
              controller.getRiskList(name, 1);
            });
            if (seriesIndex == 0) {
              return Padding(
                padding: EdgeInsets.only(top: 13.w),
                child: child,
              );
            }
            return child;
          },
          toggleSeriesVisibility: false,
        ),
        tooltipBehavior: TooltipBehavior(enable: true),
        annotations: [
          CircularChartAnnotation(
            widget: controller.initRiskCount == 0
                ? Stack(
                    alignment: Alignment.center,
                    children: [
                      annoCenter,
                      SizedBox(
                        width: 73.w,
                        height: 73.w,
                        child: CircularProgressIndicator(
                          strokeWidth: 7.w,
                          value: 1,
                          valueColor:
                              const AlwaysStoppedAnimation<Color>(Colors.grey),
                        ),
                      ),
                    ],
                  )
                : annoCenter,
          ),
        ],
        series: <CircularSeries>[
          DoughnutSeries<ChartData, String>(
            dataSource: controller.initRiskData,
            pointColorMapper: (ChartData data, _) => data.color,
            xValueMapper: (ChartData data, _) => data.x,
            yValueMapper: (ChartData data, _) => data.y,
            name: '初始风险',
            dataLabelSettings: const DataLabelSettings(
              isVisible: false,
              labelPosition: ChartDataLabelPosition.inside,
            ),
            radius: '100%',
            innerRadius: '75%',
            enableTooltip: true,
          ),
        ],
      ),
    );
  }

  /// 风险分析图表（剩余）
  Widget _buildRiskAnalysisLeft() {
    final annoCenter = Container(
      width: 56.w,
      height: 56.w,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(50.w),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            '环境监测',
            style: TextStyle(
              fontSize: 12.w,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA0A7B5),
            ),
          ),
          Text(
            '${controller.leftRiskCount}',
            style: TextStyle(
                fontSize: 16.w,
                fontWeight: FontWeight.w400,
                color: const Color(0xFF495061)),
          ),
        ],
      ),
    );
    return SizedBox(
      height: 180.w,
      child: SfCircularChart(
        legend: Legend(
          height: 130.w.toString(),
          isVisible: true,
          position: LegendPosition.right,
          alignment: ChartAlignment.center,
          overflowMode: LegendItemOverflowMode.none,
          legendItemBuilder:
              (String name, dynamic series, dynamic point, int seriesIndex) {
            final item = controller.leftRiskData[seriesIndex];
            final child = _buildLegendItem(
              item.color,
              name,
              item.y.toInt(),
              height: 19.w,
              top: 3.w,
            ).clickOnce(() {
              controller.getRiskList(name, 2);
            });
            if (seriesIndex == 0) {
              return Padding(
                padding: EdgeInsets.only(top: 13.w),
                child: child,
              );
            }
            return child;
          },
          toggleSeriesVisibility: false,
        ),
        tooltipBehavior: TooltipBehavior(enable: true),
        annotations: [
          CircularChartAnnotation(
            widget: controller.leftRiskCount == 0
                ? Stack(
                    alignment: Alignment.center,
                    children: [
                      annoCenter,
                      SizedBox(
                        width: 73.w,
                        height: 73.w,
                        child: CircularProgressIndicator(
                          strokeWidth: 7.w,
                          value: 1,
                          valueColor:
                              const AlwaysStoppedAnimation<Color>(Colors.grey),
                        ),
                      ),
                    ],
                  )
                : annoCenter,
          ),
        ],
        series: <CircularSeries>[
          DoughnutSeries<ChartData, String>(
            dataSource: controller.leftRiskData,
            pointColorMapper: (ChartData data, _) => data.color,
            xValueMapper: (ChartData data, _) => data.x,
            yValueMapper: (ChartData data, _) => data.y,
            name: '剩余风险',
            dataLabelSettings: const DataLabelSettings(
              isVisible: false,
              labelPosition: ChartDataLabelPosition.inside,
            ),
            radius: '100%',
            innerRadius: '75%',
            enableTooltip: true,
          ),
        ],
      ),
    );
  }

  /// 风险分析图例解释
  Widget _buildLegendItem(Color? color, String name, int count,
      {double? height, double? top}) {
    return Container(
      width: 100.w,
      height: height ?? 23.w,
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: top ?? 3.w),
      child: Row(
        children: [
          Container(
            width: 10.w,
            height: 10.w,
            margin: EdgeInsets.only(right: 5.w),
            padding: EdgeInsets.all(2.w),
            decoration: BoxDecoration(
                color: color, borderRadius: BorderRadius.circular(5.w)),
            child: Container(
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5.w)),
            ),
          ),
          Text(
            name,
            style: TextStyle(
                fontSize: 12.w,
                fontWeight: FontWeight.w400,
                color: const Color(0xffA0A7B5)),
          ),
          const Spacer(),
          Text(
            '$count',
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: FontWeight.w400,
              color: Colors.black,
            ),
          ),
        ],
      ),
    );
  }

  // 报警情况柱状图
  Widget _widgetBar() {
    return controller.pieData.isNotEmpty
        ? SizedBox(
            child: SfCartesianChart(
              isTransposed: true,
              legend: const Legend(
                isVisible: true,
                position: LegendPosition.top,
                overflowMode: LegendItemOverflowMode.wrap,
              ),
              // 设置x轴文字宽度
              primaryXAxis: CategoryAxis(
                // 设置x轴文字宽度
                majorGridLines: const MajorGridLines(
                  width: 0,
                ),
                majorTickLines: const MajorTickLines(
                  width: 0,
                ),
              ),
              primaryYAxis: NumericAxis(
                majorGridLines: const MajorGridLines(
                  dashArray: <double>[3, 3],
                ),
                majorTickLines: const MajorTickLines(
                  width: 0,
                ),
              ),
              plotAreaBorderWidth: 0,
              //跟踪球
              trackballBehavior: TrackballBehavior(
                //纵向选择指示器
                activationMode: ActivationMode.singleTap,
                enable: true,
                tooltipAlignment: ChartAlignment.center,
                // 线的颜色
                lineColor: const Color(0xff6DDF9A),
                //工具提示位置(顶部)
                //跟踪球始终显示(纵向选择指示器)
                tooltipDisplayMode: TrackballDisplayMode.groupAllPoints,
              ),
              series: controller.barData,
            ),
          )
        : Center(
            child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              AppGaps.vGap50,
              SvgPicture.asset('assets/svg/zhiban_empty_bg.svg'),
              Text(
                '暂无内容',
                style: TextStyle(
                  color: const Color(0xFFC7CEE5),
                  fontSize: 14.sp,
                ),
              ),
              AppGaps.vGap60,
            ],
          ));
  }

  /// 转换为列优先顺序  返回经过列优先排序的新数据列表，类型为 List<ChartData>
  /// 通过 行号 和 列号 的映射公式，将行优先的数据重新排列成列优先
  //data：原始数据列表，类型为 List<ChartData>
  //columns：需要的列数，表示最终网格的列数。
  List<ChartData> columnFirstData(List<ChartData> data, int columns) {
    final int rowCount =
        (data.length + columns - 1) ~/ columns; // 总行数  rowCount：计算每列需要容纳的行数。
    final List<ChartData> reordered =
        List.filled(data.length, data[0]); // 创建空列表
    for (int i = 0; i < data.length; i++) {
      final int col = i ~/ rowCount; // 列号
      final int row = i % rowCount; // 行号
      reordered[row * columns + col] = data[i];
    }
    return reordered;
  }

  // 人车饼图数据文字
  Widget _widgetPieText(String title, num? value, Color? color) {
    return Column(
      children: [
        Row(
          children: [
            Container(
              width: 7.w,
              height: 7.w,
              decoration: BoxDecoration(
                color: color ?? const Color(0xFF5C6DFF),
                borderRadius: BorderRadius.circular(4.w),
              ),
            ),
            Text(
              title,
              style: TextStyle(
                fontSize: 10.sp,
                color: const Color(0xFF495061),
              ),
            ),
            AppGaps.hGap2,
            Expanded(
              child: Container(
                alignment: Alignment.centerRight,
                child: Text(
                  value.toString(),
                  style: TextStyle(fontSize: 10.w),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _bottomWidget() {
    return Column(
      children: [
        _buildOnOffline(),
      ],
    );
  }

  /// 在线离线统计
  Widget _buildOnOffline() {
    final annoCenter = Container(
      width: 56.w,
      height: 56.w,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(50.w),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            '设备',
            style: TextStyle(
              fontSize: 12.w,
              fontWeight: FontWeight.w400,
              color: const Color(0xffA0A7B5),
            ),
          ),
          Text(
            '${controller.leftRiskCountOnOff}',
            style: TextStyle(
                fontSize: 16.w,
                fontWeight: FontWeight.w400,
                color: const Color(0xFF495061)),
          ),
        ],
      ),
    );
    return SizedBox(
      height: 180.w,
      child: SfCircularChart(
        legend: Legend(
          height: 130.w.toString(),
          isVisible: true,
          position: LegendPosition.right,
          alignment: ChartAlignment.center,
          overflowMode: LegendItemOverflowMode.none,
          legendItemBuilder:
              (String name, dynamic series, dynamic point, int seriesIndex) {
            final item = controller.leftRiskDataOnOff[seriesIndex];
            final child = _buildLegendItem(
              item.color,
              name,
              item.y.toInt(),
              height: 19.w,
              top: 3.w,
            ).clickOnce(() {
              controller.openOnOffDevicePage(seriesIndex);
            });
            if (seriesIndex == 0) {
              return Padding(
                padding: EdgeInsets.only(top: 13.w),
                child: child,
              );
            }
            return child;
          },
          toggleSeriesVisibility: false,
        ),
        annotations: [
          CircularChartAnnotation(
            widget: controller.leftRiskCountOnOff == 0
                ? Stack(
                    alignment: Alignment.center,
                    children: [
                      annoCenter,
                      SizedBox(
                        width: 73.w,
                        height: 73.w,
                        child: CircularProgressIndicator(
                          strokeWidth: 7.w,
                          value: 1,
                          valueColor:
                              const AlwaysStoppedAnimation<Color>(Colors.grey),
                        ),
                      ),
                    ],
                  )
                : annoCenter,
          ),
        ],
        tooltipBehavior: TooltipBehavior(enable: true),
        series: <CircularSeries>[
          DoughnutSeries<ChartData, String>(
            dataSource: controller.leftRiskDataOnOff,
            pointColorMapper: (ChartData data, _) => data.color,
            xValueMapper: (ChartData data, _) => data.x,
            yValueMapper: (ChartData data, _) => data.y,
            name: '剩余风险',
            dataLabelSettings: const DataLabelSettings(
              isVisible: false,
              labelPosition: ChartDataLabelPosition.inside,
            ),
            radius: '100%',
            innerRadius: '75%',
            enableTooltip: true,
          ),
        ],
      ),
    );
  }

  ///公司
  Widget _company(int i) {
    return Container(
      width: 120.0,
      height: 30.0,
      // margin: const EdgeInsets.only(right: 10),
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(color: const Color(0xffDCE1EA)),
          borderRadius: BorderRadius.circular(6)),
      child: Row(
        children: [
          const SizedBox(
            width: 12,
          ),
          Expanded(
              child: Text(
            controller.getString(i),
            // controller.selectCompany.isNotEmpty? controller.selectCompany:'公司名',
            style: TextStyles.grey_14,
            maxLines: 1,
          )),
          const Icon(
            Icons.keyboard_arrow_down,
            color: AppColors.font_color_gray,
          ),
          const SizedBox(
            width: 8,
          ),
        ],
      ),
    ).clickOnce(() {
      controller.selectCompanyOn(i);
    });
  }

  ///值班签到率统计
  Widget _tobWidgetTop() {
    return SizedBox(
      height: 395.w,
      child: SfCartesianChart(
        // isTransposed: true, // 横向柱状图
        legend: const Legend(
          isVisible: true,
          position: LegendPosition.top,
          overflowMode: LegendItemOverflowMode.wrap,
        ),
        // primaryXAxis: CategoryAxis(), // 设置为 CategoryAxis
        primaryXAxis: const CategoryAxis(
          majorGridLines: MajorGridLines(width: 0),
          majorTickLines: MajorTickLines(width: 0),
        ),
        primaryYAxis: const NumericAxis(
          minimum: 0,
          maximum: 100,
          interval: 20,
          labelFormat: '{value}%',
          // 显示百分比
          majorGridLines: MajorGridLines(width: 0),
          majorTickLines: MajorTickLines(width: 0),
        ),
        plotAreaBorderWidth: 0,
        //通常用于显示单个数据点的详细信息。
        // tooltipBehavior: TooltipBehavior(enable: true), // 启用工具提示
        //跟踪球  通常用于显示多个数据点的详细信息，特别是在多系列图表中非常有用。
        trackballBehavior: TrackballBehavior(
          //纵向选择指示器
          activationMode: ActivationMode.singleTap,
          enable: true,
          tooltipAlignment: ChartAlignment.center,
          // 线的颜色
          lineColor: const Color(0xff6DDF9A),
          //工具提示位置(顶部)
          //跟踪球始终显示(纵向选择指示器)
          tooltipDisplayMode: TrackballDisplayMode.groupAllPoints,
        ),
        series: [
          // 按时签到
          StackedBarSeries<AttendanceData, String>(
              dataSource: controller.attendanceDataUp,
              xValueMapper: (AttendanceData data, _) {
                return data.company;
              },
              yValueMapper: (AttendanceData data, _) => data.onTime,
              // 按时签到
              name: '按时签到',
              // color: Colors.blue,
              dataLabelSettings:  DataLabelSettings(isVisible: true,textStyle:TextStyle(color: Colors.white,fontSize: 12.sp) ),
              gradient: const LinearGradient(
                colors: [
                  Color(0x804F7EFF),
                  Color(0xFF4F7EFF),
                  Color(0xFF4F7EFF),
                ], // 渐变从深蓝到浅蓝
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
              )
              // .createShader,
              ),
          // 未签到
          StackedBarSeries<AttendanceData, String>(
              dataSource: controller.attendanceDataUp,
              xValueMapper: (AttendanceData data, _) => data.company,
              yValueMapper: (AttendanceData data, _) => data.missed,
              // 未签到
              name: '未签到',
              // color: Colors.red,
              dataLabelSettings:  DataLabelSettings(isVisible: true,textStyle:TextStyle(color: Colors.white,fontSize: 12.sp) ),
              gradient: const LinearGradient(
                colors: [
                  Color(0x80FF6868),
                  Color(0xFFFF8181),
                  Color(0xFFFF8181),
                ],
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
              )),
        ],
      ),
    );
  }

  ///值班计划执行率统计
  Widget _tobWidgetDown() {
    return SizedBox(
      height: 395.w,
      child: SfCartesianChart(
        // legend: const Legend(
        //   isVisible: true,
        //   position: LegendPosition.top,
        //   overflowMode: LegendItemOverflowMode.wrap,
        // ),
        primaryXAxis: const CategoryAxis(
          majorGridLines: MajorGridLines(width: 0),
          majorTickLines: MajorTickLines(width: 0),
        ),
        primaryYAxis: const NumericAxis(
          minimum: 0,
          maximum: 100,
          interval: 20,
          labelFormat: '{value}%',
          // 显示百分比
          majorGridLines: MajorGridLines(width: 0),
          majorTickLines: MajorTickLines(width: 0),
        ),
        plotAreaBorderWidth: 0,
        // tooltipBehavior: TooltipBehavior(enable: true), // 启用工具提示
        //跟踪球  通常用于显示多个数据点的详细信息，特别是在多系列图表中非常有用。
        trackballBehavior: TrackballBehavior(
          //纵向选择指示器
          activationMode: ActivationMode.singleTap,
          enable: true,
          tooltipAlignment: ChartAlignment.center,
          // 线的颜色
          lineColor: const Color(0xff6DDF9A),
          //工具提示位置(顶部)
          //跟踪球始终显示(纵向选择指示器)
          tooltipDisplayMode: TrackballDisplayMode.groupAllPoints,
        ),
        series: [
          StackedBarSeries<AttendanceData, String>(
            dataSource: controller.attendanceDataDown,
            xValueMapper: (AttendanceData data, _) {
              return data.company;
            },
            yValueMapper: (AttendanceData data, _) => data.onTime,
            // 按时签到
            name: '值班计划执行率统计',
            // color: Colors.blue,
            dataLabelSettings:  DataLabelSettings(isVisible: true,textStyle:TextStyle(color: Colors.white,fontSize: 12.sp) ),
              gradient: const LinearGradient(
                colors: [
                  Color(0x804F7EFF),
                  Color(0xFF4F7EFF),
                  Color(0xFF4F7EFF),
                ], // 渐变从深蓝到浅蓝
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
              )
          ),
        ],
      ),
    );
  }

  /// 报警情况饼图
// Widget _buildPieChart() {
//   final annoCenter = Container(
//     width: 56.w,
//     height: 56.w,
//     decoration: BoxDecoration(
//       color: Colors.white,
//       borderRadius: BorderRadius.circular(50.w),
//     ),
//     child: Column(
//       mainAxisAlignment: MainAxisAlignment.center,
//       children: [
//         Text(
//           '共计',
//           style: TextStyle(
//             fontSize: 12.w,
//             fontWeight: FontWeight.w400,
//             color: const Color(0xffA0A7B5),
//           ),
//         ),
//         Text(
//           '${controller.getRecordStatsListCount}',
//           style: TextStyle(
//             fontSize: 18.w,
//             fontWeight: FontWeight.w500,
//             color: const Color(0xFF495061),
//           ),
//         ),
//       ],
//     ),
//   );
//   return Row(
//     children: [
//       SizedBox(
//         height: 180.w,
//         width: 180.w,
//         //SfCircularChart 是一个圆形图组件，允许添加多个数据系列和自定义注释
//         child: SfCircularChart(
//           //annotations：用来在图表上添加自定义组件（如文字或图标）
//           annotations: [
//             CircularChartAnnotation(
//               //widget：放置在图表中心的组件，这里是一个带有文字的容器。
//               widget: controller.getRecordStatsListCount == 0
//                   ? Stack(
//                 alignment: Alignment.center,
//                 children: [
//                   annoCenter,
//                   SizedBox(
//                     width: 75.w,
//                     height: 75.w,
//                     child: CircularProgressIndicator(
//                       strokeWidth: 10.w,
//                       value: 1,
//                       valueColor: const AlwaysStoppedAnimation<Color>(Colors.grey),
//                     ),
//                   ),
//                 ],
//               )
//                   : annoCenter,
//             ),
//           ],
//           //tooltipBehavior：控制提示工具的显示属性
//           tooltipBehavior: TooltipBehavior(enable: true),
//           //series：定义图表的系列类型，这里用的是 DoughnutSeries
//           series: <CircularSeries>[
//             //DoughnutSeries 是 CircularSeries 的一种，用于绘制圆环图
//             DoughnutSeries<ChartData, String>(
//               //dataSource：数据来源，为一个列表，需包含每项的 x 和 y 值
//               dataSource: controller.riskData,
//               //pointColorMapper：定义每个数据点的颜色
//               pointColorMapper: (ChartData data, _) => data.color,
//               //xValueMapper：绑定 X 值，通常是分类名称
//               xValueMapper: (ChartData data, _) => data.x,
//               //yValueMapper：绑定 Y 值，通常是对应的数值
//               yValueMapper: (ChartData data, _) => data.y,
//               name: '报警情况',
//               dataLabelSettings: const DataLabelSettings(
//                   isVisible: false,
//                   labelPosition: ChartDataLabelPosition.inside,
//                   //标签在扇形内部，无连接线。 如果仍出现连接线，通常是由于空间不足，调整图表尺寸或半径可以解决
//                   textStyle: TextStyle(
//                     color: Colors.white,
//                   )),
//               //innerRadius 和 radius：控制圆环的内外半径
//               //通过增大 radius 或减小 innerRadius 来增加扇形的空间：如何隐藏连接线
//               radius: '100%',
//               innerRadius: '65%',
//             ),
//           ],
//         ),
//       ),
//       Expanded(
//         child: GridView.builder(
//           shrinkWrap: true,
//           // 适配父组件高度
//           physics: const NeverScrollableScrollPhysics(),
//           // 禁止滚动（由外部滚动控制）
//           gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
//             crossAxisCount: 2, // 设置为两列
//             mainAxisSpacing: 2, // 每行之间的间距
//             crossAxisSpacing: 5, // 每列之间的间距
//             childAspectRatio: 3, // 宽高比，适配子组件的宽高比例
//           ),
//           itemCount: controller.riskData.length,
//           itemBuilder: (context, index) {// 转换为列优先顺序
//            final ChartData value = columnFirstData( controller.riskData, 2)[index];
//             return _widgetPieText(value.x, value.y, value.color);
//           },
//         ),
//       )
//     ],
//   );
// }



}
